package com.sfd.message.remind.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sfd.message.remind.entity.Festival;
import com.sfd.message.remind.entity.UserRemind;
import com.sfd.message.remind.mapper.UserRemindMapper;
import com.sfd.message.remind.model.dto.CreateUserRemindDTO;
import com.sfd.message.remind.module.Result;
import com.sfd.message.remind.service.FestivalService;
import com.sfd.message.remind.service.UserRemindService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author haden
 * @date 2022/9/14 16:27
 **/
@Service
public class UserRemindServiceImpl extends ServiceImpl<UserRemindMapper, UserRemind> implements UserRemindService {
    @Resource
    private FestivalService festivalService;

    @Override
    public Result<String> createRemind(CreateUserRemindDTO dto) {
        if (dto.getUserId() == null) {
            dto.setUserId(1L);
        }
        List<Festival> festivals;
        if (dto.getFestivalId() == null) {
            festivals = festivalService.list();
        } else {
            festivals = festivalService.list(new LambdaQueryWrapper<Festival>().eq(Festival::getId, dto.getFestivalId()));
        }
        if (CollectionUtil.isNotEmpty(festivals)) {
            festivals.stream().map(t -> {
                UserRemind r = new UserRemind();
                r.setUserId(dto.getUserId());
                r.setFestivalId(t.getId());
                r.setDate(t.getDate());
                r.setName(t.getName());
                r.setType(t.getType());
                return save(r);
            }).count();
        }
        return Result.success();
    }

    @Override
    public void remind() {
        //获取所有三天内的提醒
        String now = DateUtil.today();
        String end = DateUtil.offsetDay(new Date(), 2).toDateStr();
        List<UserRemind> userReminds = list(new LambdaQueryWrapper<UserRemind>().between(UserRemind::getDate, now, end));
        if (CollectionUtil.isNotEmpty(userReminds)) {
            userReminds.stream().map(t -> {
                System.out.println(JSONObject.toJSONString(t));
                return t;
            }).collect(Collectors.toList());
        }
    }
}
